#include "widget.h"
#include "ui_widget.h"

#include "stable.h"

#include "t_my_av_utils.h"
#include "t_box_player_ctx.h"
#include "input/t_box_seq.h"
#include "input/t_box_input_sequence.h"

#include "t_youtube_dump_lib.h"

T_Box_Sequence *Widget::get_test_input()
{
    T_Box_Sequence *v_media_seq = NULL;

    //v_media_seq = get_input_file("E:/Qt/Soraya_-_Self_Control.mp4");
    //v_media_seq = get_input_file("E:/Qt/Berryz工房『恋の呪縛』縲flv");
    //v_media_seq = get_input_file("E:/Qt/6tQzHr5mgbc_34.flv");

    //v_media_seq = get_input_file("C:/6tQzHr5mgbc_34.flv");
    //v_media_seq = get_input_file("C:/temp.flv");
    //v_media_seq = get_input_youtube("hKL_EFvOLD8"); //「涙サプライズ掀を踊ってみ...
    //v_media_seq = get_input_youtube("_zjqJSCC4t4"); //Dan Balan Chica Bomb
    //v_media_seq = get_input_youtube("Ywu1DeqXTg4"); //Iron Man vs Bruce Lee
    //v_media_seq = get_input_youtube("mVEG793G3N4"); //Lady Gaga - Eh, Eh (Nothing Else I Can Say)

    //v_media_seq = get_input_youtube2("qqIIW7nxBgc", 34); //Madonna - La Isla Bonita (Video)
    //v_media_seq = get_input_youtube("qqIIW7nxBgc"); //Madonna - La Isla Bonita (Video)

    //v_media_seq = get_input_youtube("TqYen5ybVrg"); //Soraya - Self Control

    //v_media_seq = get_input_youtube("dSCtIDxOVKU"); //[HD] タイムリープぶーとべんち [Core i7 920] [GeForce 9800 GT/GE]
    v_media_seq = get_input_youtube("PZYSiWHW8V0"); //Britney Spears - Overprotected
    //v_media_seq = get_input_file("E:/Qt/Soraya_-_Self_Control.mp4");

    //v_media_seq = get_input_youtube2("TqYen5ybVrg", 34); //Soraya - Self Control
    //v_media_seq = get_input_youtube2("TqYen5ybVrg", 18); //Soraya - Self Control

    //v_media_seq = get_input_youtube2("IwljUPa05Yo", 18); //Tommy february6 - Bloomin'!

    //v_media_seq = get_input_youtube2("_QTDsV1d8rA", 18); //可憐Girl's - Over The Future / HD動画
    //v_media_seq = get_input_youtube2("_QTDsV1d8rA", 35); //可憐Girl's - Over The Future / HD動画

    //v_media_seq = get_input_youtube2("qD3_W7UFXho", 34); //[HD] 3DMark06 Demo 1+2 [Core i7 920] [GeForce 9800 GT/GE]
    //v_media_seq = get_input_youtube2("qD3_W7UFXho", 18); //[HD] 3DMark06 Demo 1+2 [Core i7 920] [GeForce 9800 GT/GE]

    //v_media_seq = get_input_youtube2("I_Ir0HrnxoM", 34); //Let Me Dream　ライブver
    //v_media_seq = get_input_youtube2("I_Ir0HrnxoM", 18); //Let Me Dream　ライブver


    //v_media_seq = get_input_youtube2("suKGwnNEwRM", 34); //m2m mirror mirror official music video
    //v_media_seq = get_input_youtube2("suKGwnNEwRM", 18); //m2m mirror mirror official music video
    //v_media_seq = get_input_youtube2("hepnvaB9F4Y", 18); //[HD] 3DMark06 Demo 3+4 [Core i7 920] [GeForce 9800 GT/GE]
    //v_media_seq = get_input_youtube2("Z07Bzw6CRZg", 34); //恋の呪文は　伊藤さやか
    //v_media_seq = get_input_youtube2("Z07Bzw6CRZg", 18); //恋の呪文は　伊藤さやか
    //v_media_seq = get_input_youtube2("2YRmr4AusRg", 34); //SWEET BLACK feat. MAKI GOTO / Lady-Rise
    //v_media_seq = get_input_youtube2("2YRmr4AusRg", 18); //SWEET BLACK feat. MAKI GOTO / Lady-Rise

    //v_media_seq = get_input_youtube2("_QTDsV1d8rA", 34); //可憐Girl's - Over The Future / HD動画
    //v_media_seq = get_input_youtube2("IwljUPa05Yo", 34); //Tommy february6 - Bloomin'!

    //v_media_seq = get_input_youtube2("X78KXfzUf_c", 34); //森下ゆうり-キューティーハニー&タッチ脀
    //v_media_seq = get_input_youtube2("X78KXfzUf_c", 18); //森下ゆうり-キューティーハニー&タッチ

    //v_media_seq = get_input_youtube2("_QTDsV1d8rA", 22); //可憐Girl's - Over The Future / HD動画
    //v_media_seq = get_input_youtube2("qD3_W7UFXho", 22); //[HD] 3DMark06 Demo 1+2 [Core i7 920] [GeForce 9800 GT/GE]
    //v_media_seq = get_input_youtube2("hepnvaB9F4Y", 22); //[HD] 3DMark06 Demo 3+4 [Core i7 920] [GeForce 9800 GT/GE]
    //v_media_seq = get_input_youtube2("-1KyXEsrnXI", 22); //モーニング娘 37 - ペッパー警部 (Close-up Ver) [道重, 田中, ジュンジュン] HD
    //v_media_seq = get_input_youtube2("Rm-0f_YafbY", 22); //AKB48 会いたかった

    //[HQ]
    //v_media_seq = get_input_youtube2("seIp4AIK2bQ", 35); //[HQ] ﾆx)- Chocolate Love MV Ver. 2

    //[HD 1080p]
    //v_media_seq = get_input_youtube2("ebrmqcuAqgU", 22); //[HD+MP4] 090718 Girls' Generation (SNSD) - Tell Me Your Wish (Genie) On Music Core
    //v_media_seq = get_input_youtube2("ebrmqcuAqgU", 37); //[HD+MP4] 090718 Girls' Generation (SNSD) - Tell Me Your Wish (Genie) On Music Core
    //v_media_seq = get_input_youtube2("sOrbHOueDn4", 22); //パトレイバ op
    //v_media_seq = get_input_youtube2("sOrbHOueDn4", 37); //パトレイバ op
    //v_media_seq = get_input_youtube2("sOrbHOueDn4", 18); //パトレイバ op
    //v_media_seq = get_input_youtube2("XLGA6FGKMfs", 18); //Orange Caramel (...[HD]
    //v_media_seq = get_input_youtube2("XLGA6FGKMfs", 22); //Orange Caramel (...[HD]
    //v_media_seq = get_input_youtube2("XLGA6FGKMfs", 37); //Orange Caramel (...[HD]
    //v_media_seq  = new T_Box_Input_Youtube("XLGA6FGKMfs", 37);
    //v_media_seq  = new T_Box_Input_Youtube("XLGA6FGKMfs", 18);
    //v_media_seq = get_input_file("C:/XLGA6FGKMfs_37_95050966.mp4");
    //v_media_seq = get_input_youtube2("bo609OUVwp0", 18); //[HD] とある科学の... OP「only my railgun」／fripSide(PV)
    //v_media_seq = get_input_youtube2("bo609OUVwp0", 37); //[HD] とある科学の... OP「only my railgun」／fripSide(PV)
    //v_media_seq = get_input_youtube2("bo609OUVwp0", 22); //[HD] とある科学の... OP「only my railgun」／fripSide(PV)
    //v_media_seq = get_input_youtube2("_i2RCBa3l-g", 22); //Avatar FULL 1080p trailer
    //v_media_seq = get_input_youtube2("_i2RCBa3l-g", 37); //Avatar FULL 1080p trailer
    //v_media_seq = get_input_youtube2("kkOiaQPm8yE", 22); //Chica Bomb - Dan Balan Music Video 1080p *HQ*
    //v_media_seq = get_input_youtube2("oDjJc9XcAkE", 22); //Britney Spears 3 Real Native HD 1080p (Download Link)
    //v_media_seq = get_input_youtube2("Thx1Wvlc5w0", 22); //[1080p][HD] Kylie Minogue - Can't Get You Out Of My Head (Blue Monday Mix Edit) [OFFICIAL MUSIC VIDEO]




    //[HD 720p]
    //v_media_seq = get_input_youtube2("aWEhonJ2xvg", 18); //Crysis 2 - Nanosuit 2 trailer
    //v_media_seq = get_input_youtube2("aWEhonJ2xvg", 22); //Crysis 2 - Nanosuit 2 trailer
    //v_media_seq = get_input_youtube2("6dwvE0SJOSo", 22); //【PV】【HD】ジンギスカン Dance Shot Ver. 高画質 Berryz工房
    //v_media_seq = get_input_youtube2("6dwvE0SJOSo", 18); //【PV】【HD】ジンギスカン Dance Shot Ver. 高画質 Berryz工房
    //v_media_seq = get_input_youtube2("AZFYSO59icI", 18); //ジンギスカン Single 16 Berryz工房 HD
    //v_media_seq = get_input_youtube2("AZFYSO59icI", 22); //ジンギスカン Single 16 Berryz工房 HD
    //v_media_seq = get_input_youtube2("AZFYSO59icI", 34); //ジンギスカン Single 16 Berryz工房 HD



    //v_media_seq = get_input_youtube2("cZGwE2pL22U", 34); //残酷な天使のテーゼ
    //v_media_seq = get_input_youtube2("8ySFADEvdBM", 34); //Robo Kiss-ロボキス[Live:Hello!Project2008Winter]Sayumi and Risako
    //v_media_seq = get_input_youtube2("8ySFADEvdBM", 18); //Robo Kiss-ロボキス[Live:Hello!Project2008Winter]Sayumi and Risako
    //v_media_seq = get_input_youtube2("5QxDuoCunF8", 34); //Berryz工房 - 16 - ジンギスカン (Ver)
    //v_media_seq = get_input_youtube2("5QxDuoCunF8", 18); //Berryz工房 - 16 - ジンギスカン (Ver)

    //v_media_seq = get_input_youtube2("ebrmqcuAqgU", 37); //[HD+MP4] 090718 Girls' Generation (SNSD) - Tell Me Your Wish (Genie) On Music Core
    //v_media_seq = get_input_youtube2("ebrmqcuAqgU", 22); //[HD+MP4] 090718 Girls' Generation (SNSD) - Tell Me Your Wish (Genie) On Music Core
    //v_media_seq = get_input_youtube2("sOrbHOueDn4", 37); //パトレイバーop
    //v_media_seq = get_input_youtube2("XLGA6FGKMfs", 37); //Orange Caramel (Magic Girl ...[HD]
    //v_media_seq = get_input_youtube2("XLGA6FGKMfs", 22); //Orange Caramel (Magic Girl ...[HD]
    //v_media_seq = get_input_file("C:/Soraya_-_Self_Control.mp4");
    //v_media_seq = get_input_youtube2("X78KXfzUf_c", 18); //森下ゆうり-キューティーハニー&タッチ
    //v_media_seq = get_input_youtube2("5QxDuoCunF8", 18); //Berryz工房 - 16 - ジンギスカン (Ver)
    //v_media_seq = get_input_youtube2("seIp4AIK2bQ", 35); //[HQ] ﾆx)- Chocolate Love MV Ver. 2
    //v_media_seq = get_input_youtube2("qD3_W7UFXho", 18); //[HD] 3DMark06 Demo 1+2 [Core i7 920] [GeForce 9800 GT/GE]
    //v_media_seq = get_input_youtube2("I_Ir0HrnxoM", 18); //Let Me Dream　ライブver
    //v_media_seq = get_input_youtube2("_i2RCBa3l-g", 22); //Avatar FULL 1080p trailer
    //v_media_seq = get_input_youtube2("kkOiaQPm8yE", 22); //Chica Bomb - Dan Balan Music Video 1080p *HQ*
    //v_media_seq = get_input_youtube2("oDjJc9XcAkE", 22); //Britney Spears 3 Real Native HD 1080p (Download Link)
    //v_media_seq = get_input_youtube2("Thx1Wvlc5w0", 22); //[1080p][HD] Kylie Minogue - Can't Get You Out Of My Head (Blue Monday Mix Edit) [OFFICIAL MUSIC VIDEO]
    //v_media_seq = get_input_youtube2("Thx1Wvlc5w0", 37); //[1080p][HD] Kylie Minogue - Can't Get You Out Of My Head (Blue Monday Mix Edit) [OFFICIAL MUSIC VIDEO]

    return v_media_seq;

}


T_Box_Sequence *Widget::get_input_file(const QString &a_filename)
{
    T_Box_Sequence_File *v_seq_file = new T_Box_Sequence_File(a_filename);
    return v_seq_file;
}

T_Box_Sequence *Widget::get_input_youtube(const QString &a_video_id)
{
    QMap<int, QUrl> v_fmt_map;
    QStringList v_error_list;
    if(!T_Youtube_Dump_Lib::getVideoStreamMap(&m_nam, a_video_id, v_fmt_map, v_error_list))
    {
        return NULL;
    }

    T_NetworkBatch v_nbat(&m_nam);

    //int v_fmt = v_fmt_map.value(22).isValid() ? 22 : 34;
    int v_fmt = v_fmt_map.value(34).isValid() ? 34 : 18;
    QVariant v_loc = v_fmt_map.value(v_fmt);
    if(v_loc.isValid())
    {
        QString v_removed_url = QUrl::fromPercentEncoding(v_loc.toString().toLatin1());
        v_loc = v_removed_url;
    }
    QNetworkReply *v_reply = NULL;
#if 0x1
    while(v_loc.isValid())
    {
        delete v_reply;
        qDebug() << "[v_loc]" << v_loc;
        v_reply = v_nbat.head(v_loc.toUrl());
        v_loc = v_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
        qDebug() << "";
        qDebug() << "RedirectionTargetAttribute" << v_loc;
        qDebug() << "HttpStatusCodeAttribute" << v_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
        qDebug() << "HttpReasonPhraseAttribute" << v_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute);
        qDebug() << "RedirectionTargetAttribute" << v_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
        qDebug() << "ContentTypeHeader" << v_reply->header(QNetworkRequest::ContentTypeHeader);
        qDebug() << "ContentLengthHeader" << v_reply->header(QNetworkRequest::ContentLengthHeader);
        qDebug() << "LocationHeader" << v_reply->header(QNetworkRequest::LocationHeader);
        foreach(QByteArray v_first, v_reply->rawHeaderList())
        {
            qDebug() << v_first << v_reply->rawHeader(v_first);
        }
        qDebug() << "";
    }

    QVariant v_cont_header = v_reply->header(QNetworkRequest::ContentTypeHeader);
    if(!v_cont_header.isValid())
    {
        qDebug() << "(!v_cont_header.isValid())";
        return NULL;
    }

    qDebug() << v_cont_header;

    if(!v_cont_header.toString().startsWith("video/"))
    {
        qDebug() << "(!v_cont_header.toString().startsWith(\"video/\"))";
        return NULL;
    }
#endif


    qDebug() << "ok";

    qDebug() << "[Widget::get_input_youtube()]v_loc.toUrl()" << v_loc.toUrl();
    qDebug() << "[Widget::get_input_youtube()]v_reply->url()" << v_reply->url();
    QNetworkRequest v_video_request(v_reply->url());
    //QNetworkRequest v_video_request(v_loc.toUrl());
    QNetworkReply *v_video_reply = m_nam.get(v_video_request);
    T_Box_Sequence *v_seq = new T_Box_Sequence_Http(v_video_reply);
    v_seq->displayName(a_video_id+":"+QString::number(v_fmt));
    return v_seq;
}

T_Box_Sequence *Widget::get_input_youtube2(const QString &a_video_id, int a_fmt)
{
    return 0;
}

void Widget::test_dump_format_ctx(AVFormatContext *a_format_ctx)
{
    QString v_print;
    //[Format Name]
    qDebug() << v_print.sprintf(
            "formatCtx->iformat->long_name=%s (->name=%s)"
            , a_format_ctx->iformat->long_name
            , a_format_ctx->iformat->name
            );
    //[Meta Data]
    AVMetadata *m = a_format_ctx->metadata;
    if(m && !(m->count == 1 && av_metadata_get(m, "language", NULL, 0))){
        AVMetadataTag *tag=NULL;
        av_log(NULL, AV_LOG_INFO, "%sMetadata:\n", "  ");
        while((tag=av_metadata_get(m, "", tag, AV_METADATA_IGNORE_SUFFIX))) {
            if(strcmp("language", tag->key))
                av_log(NULL, AV_LOG_INFO, "%s  %-16s: %s\n", "  ", tag->key, tag->value);
        }
    }

    //[nb_programs]
    qDebug() << QString("formatCtx->nb_programs=%1").arg(a_format_ctx->nb_programs);

    //[nb_chapters]
    qDebug() << QString("formatCtx->nb_chapters=%1").arg(a_format_ctx->nb_chapters);

    //[nb_streams]
    qDebug() << QString("formatCtx->nb_streams=%1").arg(a_format_ctx->nb_streams);

    for(int i=0;i<(int)a_format_ctx->nb_streams;i++)
    {
        my_dump_stream_format(a_format_ctx, i, 0, false);
        AVStream *st = a_format_ctx->streams[i];
        int g = av_gcd(st->time_base.num, st->time_base.den);
        av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d\n", st->codec_info_nb_frames, st->time_base.num/g, st->time_base.den/g);
        char buf[256];
        my_avcodec_string(buf, sizeof(buf), st->codec, false);
        qDebug() << buf;
    }
}
